<script setup lang="ts">
import { ref } from "vue";
import closeIcon from "@/assets/images/close.png";
import {
  deleteAreaById,
  getSysDictionaryByType,
  queryAreaInfoList,
} from "@/api/pipe";
import {useLocalStore} from "@/views/layer/area/store";
const isShowInfoList = ref(true);
const tableData = ref([]);
const currentPage = ref(1);
const total = ref(0);
const background = ref(false);
const disabled = ref(false);
const typeOptions = ref<Array<{ value: number; label: string }>>([]);
const polygonType = ref<null | number>(null);
const emitEvents = defineEmits(["close"]);
const localStore = useLocalStore();
getSysDictionaryByType({
  pageSize: 1000,
  pageIndex: 1,
  dicTypeId: 1,
}).then(({ data, succeeded }) => {
  if (succeeded) {
    typeOptions.value = [{value: -1, label: '全部'}].concat(data.items.map((item: any) => {
      const { dictName, dictId } = item;
      return {
        value: dictId,
        label: dictName,
      };
    }));
  }
});
const handleCurrentChange = (val: number) => {
  currentPage.value = val;
  getData();
};
const handleDelete = (index: number, row: any) => {
  const { polygonId } = row;
  deleteAreaById({ polygonId }).then(({ succeeded }) => {
    if (succeeded) {
      ElMessage("删除成功！");
      getData();
      localStore.increaseUpdateFlagCount();
    } else {
      ElMessage("删除失败！");
    }
  });
};
const onTypeChange = (polygonId: number) => {
  const { setAreaTypeId } = props;
  polygonType.value = polygonId;
  getData();
  setAreaTypeId && setAreaTypeId(polygonId);
};
const getData = () =>
  queryAreaInfoList({
    pageIndex: currentPage.value,
    pageSize: 5,
    polygonType: polygonType.value === -1 ? null : polygonType.value,
  }).then(({ data, succeeded }) => {
    if (succeeded) {
      tableData.value = data.items.map((item: any, index: number) => ({
        ...item,
        index: index + 1,
        area: (item.polygonSize).toFixed(4),
      }));
      total.value = data.totalCount;
    }
  });
onMounted(() => getData());
const props = defineProps({
  setAreaTypeId: {
    type: Function,
    default: () => 0,
  },
});
</script>

<template>
  <div class="info-list" v-if="isShowInfoList">
    <div class="title">
      <span>区域列表</span>
      <img class="close" :src="closeIcon" @click="emitEvents('close')" />
    </div>
    <div class="search">
      <el-select
        v-model="polygonType"
        placeholder="请选择区域类型"
        @change="onTypeChange"
        style="width: 250px; margin-right: 10px"
      >
        <el-option
          v-for="item in typeOptions"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        />
      </el-select>
      <el-button type="primary" round style="width: 100px" @click="getData()"
        >查询</el-button
      >
    </div>
    <el-table :data="tableData" style="width: 100%; height: 250px">
      <el-table-column label="序号" width="13%">
        <template #default="scope">
          <div style="display: flex; align-items: center">
            <span>{{ scope.row.index }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="名称" width="25%">
        <template #default="scope">
          <el-tooltip
            class="box-item"
            effect="dark"
            :content="scope.row.polygonName"
            placement="top"
          >
            <div style="display: flex; align-items: center">
              <span class="label">{{ scope.row.polygonName }}</span>
            </div>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="编号" width="20%">
        <template #default="scope">
          <el-tooltip
            class="box-item"
            effect="dark"
            :content="scope.row.polygonNo"
            placement="top"
          >
            <div style="display: flex; align-items: center">
              <span class="label" style="color: blue">{{
                scope.row.polygonNo
              }}</span>
            </div>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="面积(km²)" width="22%">
        <template #default="scope">
          <el-tooltip
            class="box-item"
            effect="dark"
            :content="scope.row.area"
            placement="top"
          >
            <div style="display: flex; align-items: center">
              <span class="label">{{ scope.row.area }}</span>
            </div>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="20%">
        <template #default="scope">
          <el-popconfirm
            title="确定删除吗?"
            @confirm="handleDelete(scope.$index, scope.row)"
          >
            <template #reference>
              <el-button size="small" type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div class="pager">
      <el-pagination
        :small="true"
        v-model:current-page="currentPage"
        :page-size="5"
        :pager-count="5"
        :disabled="disabled"
        :background="background"
        layout="total, prev, pager, next"
        :total="total"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<style scoped lang="scss">
.info-list {
  position: absolute;
  top: 255px;
  right: 55px;
  z-index: 1000;
  width: 420px;
  height: auto;
  font-family: Roboto, sans-serif;
  font-size: 14px;
  line-height: 20px;
  color: rgb(16 16 16 / 100%);
  text-align: center;
  background-color: rgb(255 255 255 / 100%);
  border: 1px solid rgb(187 187 187 / 100%);

  :deep(.el-table__header) {
    width: 100% !important;
  }

  :deep(.el-table__body) {
    width: 100% !important;
  }

  :deep(.el-table__cell) {
    height: 40px !important;
  }
}

.title {
  height: 40px;
  padding-left: 10px;
  font-size: 20px;
  background: #1990ff 100%;

  span {
    line-height: 40px;
    color: #fff;
  }

  .close {
    position: relative;
    top: 4px;
    right: -130px;
    cursor: pointer;
  }
}

.search {
  margin: 8px 5px 10px 14px;
}

.label {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.pager {
  float: right;
  margin: 10px;
}
</style>
